package com.lanou3g.demo.dao;

import com.lanou3g.demo.db.DBConnector;
import com.lanou3g.demo.db.EntityHandler;
import com.lanou3g.demo.db.EntityListHandler;
import com.lanou3g.demo.entity.BookEntity;
import com.lanou3g.demo.entity.OrderEntity;
import com.lanou3g.demo.entity.OrderItemEntity;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDao {

    // 根据用户id查询用户的订单
    public static OrderEntity selectOrder(String uid) throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from orders where uid = ?";
        OrderEntity order = runner.query(conn, sql, new EntityHandler<>(OrderEntity.class), uid);
        return order;
    }

    // 通过订单id找到所有的订单项
    public static List<OrderItemEntity> selectOrderItem(String oid) throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from orderitem where oid = ?";
        List<OrderItemEntity> orderItemList = runner.query(conn, sql, new EntityListHandler<>(OrderItemEntity.class), oid);
        return orderItemList;
    }

    // 通过bid查询书籍信息
    public static BookEntity selectBook(String bid) throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from book where bid = ?";
        BookEntity book = runner.query(conn, sql, new EntityHandler<>(BookEntity.class), bid);
        return book;
    }

    // 查询数据库中所有的订单
    public static List<OrderEntity> selectAllOrder() throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from orders";
        List<OrderEntity> orderList = runner.query(conn, sql, new EntityListHandler<>(OrderEntity.class));
        return orderList;
    }

    // 查看数据库中未付款订单（1）
    public static List<OrderEntity> selectWF() throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from orders where state = 1";
        List<OrderEntity> orderList = runner.query(conn, sql, new EntityListHandler<>(OrderEntity.class));
        return orderList;
    }

    // 查看数据库中已付款订单（2）
    public static List<OrderEntity> selectYF() throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from orders where state = 2";
        List<OrderEntity> orderList = runner.query(conn, sql, new EntityListHandler<>(OrderEntity.class));
        return orderList;
    }

    // 查看数据库中未收货订单（3）
    public static List<OrderEntity> selectWS() throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from orders where state = 3";
        List<OrderEntity> orderList = runner.query(conn, sql, new EntityListHandler<>(OrderEntity.class));
        return orderList;
    }

    // 查看数据库中已完成订单（4）
    public static List<OrderEntity> selectYS() throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from orders where state = 4";
        List<OrderEntity> orderList = runner.query(conn, sql, new EntityListHandler<>(OrderEntity.class));
        return orderList;
    }


}
